import { EditorView } from "codemirror";
export { undo, redo } from '@codemirror/commands';

export const getText = (view: EditorView) => {
    if (!view) return '';
    return view.state.doc.toString();
};
export const setText = (view: EditorView, text: string) => {
    if (!view) return;
    const text_old = getText(view);
    if (text != text_old) {
        view.dispatch({
            changes: {
                from: 0,
                to: text_old.length,
                insert: text,
            },
        });
    }
};
export const insertText = (view: EditorView, text: string, pos = -1) => {
    if (!view) return;
    const transaction = view.state.replaceSelection(text);
    view.dispatch(transaction);
};
